package com.novellotus.iov.carvice.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.bean.RxDiagnosisAlarm;
import com.novellotus.iov.carvice.utils.MyUtils;

import butterknife.BindView;

/**
 * 电流图
 */
public class CurrentGraphFragment extends BaseFragment {


    //bms
    @BindView(R.id.rlt_bms)
    RelativeLayout rlt_bms;
    ImageView img_bms;
    ImageView img_bms_triangle;
    TextView tv_bms_word;
    //T-box
    @BindView(R.id.rlt_box)
    RelativeLayout rlt_box;
    ImageView img_box;
    ImageView img_box_triangle;
    TextView tv_box_word;
    //VCU
    @BindView(R.id.rlt_vcu)
    RelativeLayout rlt_vcu;
    ImageView img_vcu;
    ImageView img_vcu_triangle;
    TextView tv_vcu_word;
    //DC
    @BindView(R.id.rlt_dc)
    RelativeLayout rlt_dc;
    ImageView img_dc;
    ImageView img_dc_triangle;
    TextView tv_dc_word;
    //电机
    @BindView(R.id.rlt_motor)
    RelativeLayout rlt_motor;
    ImageView img_motor;
    TextView tv_motor_word;
    //控制器
    @BindView(R.id.rlt_control)
    RelativeLayout rlt_control;
    ImageView img_control;
    TextView tv_control_word;
    //高压盒
    @BindView(R.id.rlt_height_box)
    RelativeLayout rlt_height_box;
    ImageView img_height_box;
    TextView tv_height_box_word;

    @BindView(R.id.tv_plate)
    TextView tv_plate;
    @BindView(R.id.tv_vin)
    TextView tv_vin;
    @BindView(R.id.tv_height_voltage_value)
    TextView tv_height_voltage_value;
    @BindView(R.id.tv_low_voltage_value)
    TextView tv_low_voltage_value;
    @BindView(R.id.tv_height_temperature_value)
    TextView tv_height_temperature_value;
    @BindView(R.id.tv_low_temperature_value)
    TextView tv_low_temperature_value;


    @BindView(R.id.rlt_current_box)
    RelativeLayout rlt_current_box;
    @BindView(R.id.tv_current_box_word)
    TextView tv_current_box_word;

    @BindView(R.id.tv_control_voltage)
    TextView tv_control_voltage;

    @BindView(R.id.tv_control_current)
    TextView tv_control_current;

    @BindView(R.id.tv_height_box_voltage)
    TextView tv_height_box_voltage;

    @BindView(R.id.tv_height_box_current)
    TextView tv_height_box_current;

    private RxDiagnosisAlarm rxDiagnosisAlarm;

    public CurrentGraphFragment() {
        // Required empty public constructor
    }

    public static CurrentGraphFragment newInstance(RxDiagnosisAlarm rxDiagnosisAlarm) {
        CurrentGraphFragment fragment = new CurrentGraphFragment();
        Bundle args = new Bundle();
        args.putParcelable("alarm", rxDiagnosisAlarm);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            rxDiagnosisAlarm = getArguments().getParcelable("alarm");
        }
    }

    @Override
    View initRootView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_current_graph, container, false);
    }

    private void initViews() {
        //bms
        img_bms = rlt_bms.findViewById(R.id.img_background);
        img_bms.setBackgroundResource(R.mipmap.icon_bms_white);
        img_bms_triangle = rlt_bms.findViewById(R.id.img_triangle);
        tv_bms_word = rlt_bms.findViewById(R.id.tv_word);
        //t-box
        img_box = rlt_box.findViewById(R.id.img_background);
        img_box.setBackgroundResource(R.mipmap.icon_box_white);
        img_box_triangle = rlt_box.findViewById(R.id.img_triangle);
        tv_box_word = rlt_box.findViewById(R.id.tv_word);
        //vcu
        img_vcu = rlt_vcu.findViewById(R.id.img_background);
        img_vcu_triangle = rlt_vcu.findViewById(R.id.img_triangle);
        tv_vcu_word = rlt_vcu.findViewById(R.id.tv_word);
        img_vcu.setBackgroundResource(R.mipmap.icon_vcu_white);
        //DC
        img_dc = rlt_dc.findViewById(R.id.img_background);
        img_dc.setBackgroundResource(R.mipmap.icon_dc_white);
        img_dc_triangle = rlt_dc.findViewById(R.id.img_triangle);
        tv_dc_word = rlt_dc.findViewById(R.id.tv_word);
        //电机
        img_motor = rlt_motor.findViewById(R.id.img_background);
        img_motor.setBackgroundResource(R.mipmap.icon_motor_white);
        tv_motor_word = rlt_motor.findViewById(R.id.tv_word);
        //控制器
        img_control = rlt_control.findViewById(R.id.img_background);
        img_control.setBackgroundResource(R.mipmap.icon_control_white);
        tv_control_word = rlt_control.findViewById(R.id.tv_word);
        //高压盒
        img_height_box = rlt_height_box.findViewById(R.id.img_background);
        img_height_box.setBackgroundResource(R.mipmap.icon_height_box_white);
        tv_height_box_word = rlt_height_box.findViewById(R.id.tv_word);
        //电池
        rlt_current_box.setBackgroundResource(R.mipmap.icon_current_white_box);
    }

    @Override
    void init() {
        initViews();
        if (rxDiagnosisAlarm != null) {
            tv_height_box_voltage.setText(MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getTotal_voltage() + "") + "V");
            tv_height_box_current.setText(MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getTotal_current() + "") + "A");
//            tv_control_voltage.setText(MyUtils.getRealValue(rxDiagnosisAlarm.getModelValues().getControl_in_voltage() + "") + "V");
//            tv_control_current.setText(MyUtils.getRealValue(rxDiagnosisAlarm.getModelValues().getControl_bus_current() + "") + "A");
            tv_plate.setText(rxDiagnosisAlarm.getPn());
            tv_vin.setText(rxDiagnosisAlarm.getVin());

            tv_height_voltage_value.setText(MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getBattery_single_highest_voltage() + "") + "V  " + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getHighest_voltage_battery_subsys_num()) + "/" + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getHighest_voltage_battery_single_num()));
            tv_low_voltage_value.setText(MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getBattery_single_lowest_voltage() + "") + "V  " + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getLowest_voltage_battery_subsys_num()) + "/" + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getLowest_voltage_battery_single_num()));
            tv_height_temperature_value.setText(MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getHighest_temp() + "") + "℃  " + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getHighest_temp_battery_subsys_num()) + "/" + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getHighest_temp_battery_probe_num()));
            tv_low_temperature_value.setText(MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getLowest_temp() + "") + "℃  " + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getLowest_temp_battery_subsys_num()) + "/" + MyUtils.getRealNumberValue(rxDiagnosisAlarm.getModelValues().getLowest_temp_battery_probe_num()));
        }
        setAlarmComponents();
        img_dc_triangle.setVisibility(View.INVISIBLE);
    }

    private void setAlarmComponents() {

        for (int i = 0; i < rxDiagnosisAlarm.getComponents().size(); i++) {
            switch (rxDiagnosisAlarm.getComponents().get(i).getCompCode()) {
                case "comp_motor":
                    //电机
                    img_motor.setBackgroundResource(R.mipmap.icon_motor_blue);
                    tv_motor_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    tv_motor_word.setVisibility(View.VISIBLE);
                    break;
                case "comp_controller":
                    //控制器
                    img_control.setBackgroundResource(R.mipmap.icon_control_blue);
                    tv_control_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    tv_control_word.setVisibility(View.VISIBLE);
                    break;
                case "comp_high_voltage":
                    //高压盒
                    img_height_box.setBackgroundResource(R.mipmap.icon_height_box_blue);
                    tv_height_box_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    tv_height_box_word.setVisibility(View.VISIBLE);
                    break;
                case "comp_bms":
                    //BMS
                    img_bms.setBackgroundResource(R.mipmap.icon_bms_blue);
                    tv_bms_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    tv_bms_word.setVisibility(View.VISIBLE);
                    break;
                case "comp_tbox":
                    //TBOX
                    img_box.setBackgroundResource(R.mipmap.icon_box_blue);
                    tv_box_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    tv_box_word.setVisibility(View.VISIBLE);
                    break;
                case "comp_vcu":
                    //VCU
                    img_vcu.setBackgroundResource(R.mipmap.icon_vcu_blue);
                    tv_vcu_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    tv_vcu_word.setVisibility(View.VISIBLE);
                    break;
                case "comp_dcdc":
                    //DC-DC
                    img_dc.setBackgroundResource(R.mipmap.icon_dc_blue);
                    tv_dc_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    tv_dc_word.setVisibility(View.VISIBLE);
                    break;
                case "comp_battery":
                    //电池
                    rlt_current_box.setBackgroundResource(R.mipmap.icon_current_blue_box);
                    tv_current_box_word.setVisibility(View.VISIBLE);
                    tv_current_box_word.setText(rxDiagnosisAlarm.getAlarms().get(i).getDisplayCode());
                    break;
            }
        }

    }


}
